<template>
  <div>
    <el-card class="box-card">
      <h2>编辑客户</h2>
      <el-form :model="customerForm" label-width="120px">
        <el-form-item label="客户名称：">
          <el-input v-model="customerForm.name" readonly></el-input>
        </el-form-item>
        <el-form-item label="修改客户名称："required>
                  <el-input v-model="newCustomerName"></el-input>
                </el-form-item>
        <el-form-item label="电话："required>
          <el-input v-model="customerForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="地址："required>
          <el-input v-model="customerForm.address"></el-input>
        </el-form-item>
        <el-form-item label="传真：">
          <el-input v-model="customerForm.fax"></el-input>
        </el-form-item>
        <el-form-item label="邮箱：">
          <el-input v-model="customerForm.email" type="email"></el-input>
        </el-form-item>
        <el-form-item label="银行名称：">
          <el-input v-model="customerForm.bankName"></el-input>
        </el-form-item>
        <el-form-item label="银行账号：">
          <el-input v-model="customerForm.bankAccount"></el-input>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input v-model="customerForm.remarks" type="textarea"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
             <el-button @click="resetForm('customerForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      customerForm: {
        name:  this.$route.params.customerName,
        phone: '',
        address: '',
        fax: '',
        email: '',
        bankName: '',
        bankAccount: '',
        remarks: ''
      },
      customerName: '',
      newCustomerName: ''
    };
  },
  created() {
    this.customerName = this.$route.params.customerName || '';
    if (this.customerName) {
      this.fetchCustomerDetailsByName(this.customerName);
    } else {
      console.error('Error: customerName is empty.');
    }
  },
  methods: {
    fetchCustomerDetailsByName(customerName) {
       this.axios.get(`/api/customer/search?name=${customerName}`)
        .then(response => {
          console.log(response.data);  // 检查响应数据
          const customerDetails = response.data.data || {};
              const customer= customerDetails[0]; // 获取数组中的第一个元素
          this.customerForm = {
            name: customer.name || '',
            phone: customer.phone || '',
            address: customer.address || '',
            fax: customer.fax || '',
            email: customer.email || '',
            bankName: customer.bankName || '',
            bankAccount: customer.bankAccount || '',
            remarks: customer.remarks || ''
          };
          this.newCustomerName = customer.name || '';
        })
        .catch(error => {
          console.error('Error fetching customer details:', error);
        });
    },
    submitForm() {
      console.log('Submitting form with data:', this.customerForm);  // 打印提交的数据
      const updatedCustomerForm = { ...this.customerForm, name: this.newCustomerName };
      this.axios.put(`/api/customer/edit/${this.customerName}`, updatedCustomerForm)
        .then(response => {
          console.log('Server response:', response);  // 打印服务器响应
          this.$message({
            message: '客户更新成功',
            type: 'success'
          });
          this.$router.push('/list_customer');
        })
        .catch(error => {
          console.error('Error updating customer:', error);
          console.error('Error response:', error.response);  // 打印错误响应
          this.$message.error('客户更新失败');
        });
    },
       resetForm() {
          // 重置表单
         this.customerForm = {
                                 phone: '',
                                 address: '',
                                 fax: '',
                                 email: '',
                                 bankName: '',
                                 bankAccount: '',
                                 remarks: ''
                  };
        }
  }
};
</script>

<style scoped>
.box-card {
  margin: auto;
  width: 600px;
}
.el-form-item {
  margin-bottom: 20px;
}
</style>
